<template>

  <div>
    <div class="goods-container">
      <!-- 循环生成商品 -->
      <div v-for="(good,index) in goods" v-if="(num-1)*12<=index&&index<num*12" class="goods-item-box">
        <div class="goods-item">
          <img class="goods-img" v-bind:src="['https://oss.gaojie.cc/'+ good.image]">
          <span class="goods-title">{{ good.title }}</span>
          <span class="goods-price">{{ good.price }}￥</span>
          <span class="goods-footer">
          <i @click="sendGoodsId($event)" class="fa fa-pencil" :name="good.id"></i>
          <i @click="delGoods($event)" :name="good.id" class="fa fa-trash"></i>
        </span>
        </div>
      </div>
    </div>

    <!--  分页按钮-->
    <div style="width: 480px; height: 100px; margin: 20px auto;">
      <div class="input-num">
        <button @click="sub">上一页</button>
        <span>{{ num }}</span>
        <button @click="add">下一页</button>
      </div>
    </div>
  </div>

</template>

<script>
module.exports = {
  data() {
    return {
      goodsName: '',
      isShow: true,
      goods: {},
      num:1
    };
  },
  mounted: function () {
    axios.get("/goodsblock").then((response) => {
      this.goods = response.data;
    });
  },
  methods: {
    delGoods(event) {
      console.log(event.target.getAttribute('name'));
      axios.get("/goodsdelete", {
        params: {
          goodsId: event.target.getAttribute('name'),
        }
      }).then((response) => {
        // 后端传递给前端的数据在 response.data 中
        this.$forceUpdate();
        if (response.data == "OK") {
          //页面不能跳转
          this.$forceUpdate();
          this.$emit("show-me-goods", 2);
          this.$emit("show-me-goods", 1);
        }
      });
      this.$forceUpdate();
    },
    sendGoodsId(event) {
      // 传id值给商品添加页面并 子--》父--》子
      var goodsId = event.target.getAttribute('name')
      this.$emit("send-goods-id", goodsId);
    },
    sub: function () {
      if (this.num > 1) {
        this.num--;
      } else {
        // alert("别点啦,太小啦!");
      }
    },
    add: function () {
      // console.log("add");
      // 累加
      if (this.num < Math.ceil(Object.keys(this.goods).length/12)) {
        this.num++;
      } else {
        alert("没有更多了!");
      }
    }
  }
}
</script>

<style scoped>
.goods-container {
  width: 1250px;
  height: 500px;
}

.goods-item-box {
  display: inline-block;
  background-color: #ffffff;
  margin-bottom: 14px;
  margin-left: 25px;
}

.goods-item {
  display: inline-block;
  width: 150px;
  background-color: #FFF;
  border-radius: 6px;
  margin: 2px;
  padding: 10px;
  border: 2px rgb(0, 255, 128) solid;
  border-radius: 3px;
  height: 210px;
}

.goods-item span {
  display: block;
}

.goods-item .goods-img {
  width: 150px;
  height: 150px;
  margin: 0px auto;
  display: block;
  max-width: 100%;
}

.goods-item .goods-footer {
  text-align: right;
}

.input-num {
  margin-top: 31px;
  height: 41%;
  display: flex;
  border-radius: 2px;
  overflow: hidden;
  box-shadow: 0 0 4px black;
}

.input-num button {
  width: 172px;
  height: 100%;
  font-size: 24px;
  color: gray;
  cursor: pointer;
  border: none;
  outline: none;
  background-color: black;
}

.input-num span {
  height: 50%;
  font-size: 35px;
  flex: 1;
  text-align: center;
  line-height: 47px;
}
</style>